/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.widgets.listview;

import java.io.Serializable;

import android.content.Context;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.phonebooksharing.android.R;
import com.phonebooksharing.android.async.LoadAndroidPhotoTask;
import com.phonebooksharing.android.async.LoadNetworkImageTask;
import com.phonebooksharing.android.util.GalleryUtilities;
import com.phonebooksharing.android.util.GeneralUtils;
import com.phonebooksharing.android.util.Global;
import com.phonebooksharing.android.widgets.drawable.FastBitmapDrawable;
import com.phonebooksharing.dto.dashboard.DashboardReaderItem;
import com.phonebooksharing.dto.friend.ContactItem;
import com.phonebooksharing.dto.gallery.PostCardItem;

@SuppressWarnings("unchecked")
public class PostcardListView extends ReadableListView {
    
    private LayoutInflater inflater;
    public PostcardListView(Context dv, int qty, TextView counter) {
        super(dv, qty, counter);
        inflater = LayoutInflater.from(getContext());
    }
    
    @Override
    public View getConvertView(View convertView, Object dto, int position, ViewGroup parent) {
       ViewHolder holder;
       
       final DashboardReaderItem<ContactItem> d = (DashboardReaderItem<ContactItem>)dto;
       final PostCardItem cache = (PostCardItem)d.getItem();
       
       if (convertView == null) {
           convertView = inflater.inflate(R.layout.list_item_3lines_2icon_check, null);

           holder = new ViewHolder();
           holder.icon = (ImageView) convertView.findViewById(R.id.icon);
           holder.icon2 = (ImageView) convertView.findViewById(R.id.icon2);
           holder.name = (TextView) convertView.findViewById(R.id.name);
           holder.time = (TextView) convertView.findViewById(R.id.date);
           holder.text1 = (TextView) convertView.findViewById(R.id.title);
           holder.text2 = (TextView) convertView.findViewById(R.id.text);
           
           holder.type = (ImageView) convertView.findViewById(R.id.type);
           holder.check = (ImageView) convertView.findViewById(R.id.check);
           
           holder.container = convertView.findViewById(R.id.container);
           holder.container.setOnClickListener(checkListener);
           
           convertView.setTag(holder);
       } else {
           holder = (ViewHolder) convertView.getTag();
       }
       holder.container.setTag(d);
       
       final FastBitmapDrawable icon = GalleryUtilities.getFromContactCache(cache.andID);
       if(icon != null) {
           holder.icon.setImageBitmap(icon.getBitmap());
       } else {
           holder.icon.setImageBitmap(GalleryUtilities.loadingBitmap.getBitmap());
           long andID;
           if((andID = cache.andID) > 0) {
               new LoadAndroidPhotoTask(this).execute(andID, position);
           } else {
               new LoadNetworkImageTask(this).execute(cache.iconID, 60, 60, position);
           }
       }
       
       final long postcardID = cache.postcardID;
       final FastBitmapDrawable photo = GalleryUtilities.getFromPostcardThumbCache(postcardID);
       if(photo != null) {
           holder.icon2.setImageBitmap(photo.getBitmap());
       } else {
           holder.icon2.setImageBitmap(GalleryUtilities.loadingBitmap.getBitmap());
           final String url = String.format("http:" + Global.IMAGE_SERVLET_URL, "gpcf" /*ac*/, postcardID /*q*/, "" /*c*/, "" /*p*/, "" /*ps*/, "" /*mt*/, 60 /*w*/, 60 /*h*/);
           new LoadNetworkImageTask(this, url, false).execute(postcardID, 60, 60, position, true /*isThumbnail*/, R.id.icon2);
       }
       
       if(d.isCrossed()) {
           holder.check.setImageState(new int[] { android.R.attr.state_checked }, true);
       } else {
           holder.check.setImageState(new int[] {  }, true);
       }
       
       holder.id = d.getId();
       holder.text1.setText(cache.title);
       holder.name.setText(cache.name);
       holder.time.setText("(" + DateUtils.formatElapsedTime((GeneralUtils.calculateGmtTimeInMilis(System.currentTimeMillis()) - 
                                                              GeneralUtils.calculateGmtTimeInMilis(d.getWhen()) ) / 1000) + ")");
       
       return convertView;
    }
    
    @Override
    public String getLoadDataURL() {
        return String.format(Global.DASHBOARD_SERVLET_URL, "pc" /*ac*/, "" /*q*/, currentPage++ /*p*/, Global.PAGINATOR_PAGE_SIZE /*ps*/);
    }

    @Override
    public Serializable getObject2Send() {
        return Global.authItem;
    }
}
